﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Data;
using UIT.Controls.Containers;

namespace UIT.Web.Renderers
{
    class ItemsControlRenderer : ControlRenderer<ItemsControl>, IControlRenderer<SelectableItemsControl>
    {
        public override void Render(ItemsControl control)
        {
            IContainer panel;
            if (control.ItemsPanel == null)
            {
                panel = new StackPanel();
                panel.StyleKey = control.StyleKey;
            }
            else
                panel = (IContainer)control.ItemsPanel.Template.CreateInstance();
            if (control != null && control.DataSource != null)
                foreach (var item in control.DataSource.Select(new SelectArgument()))
                    ((IControlContainer)panel).Children.Add(control.ItemTemplate.CreateInstance(item));
            Manager.Render(panel);
        }

        #region IRenderer<SelectableItemsControl> Members

        public void Render(SelectableItemsControl control)
        {
            Render((ItemsControl)control);
        }

        #endregion

        #region IVisitor<SelectableItemsControl> Members

        public void Visit(SelectableItemsControl visitable)
        {
            Render(visitable);
        }

        #endregion
    }
}
